from datetime import datetime

from exts import db

class Usermodel(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(20), nullable=False)
    email = db.Column(db.String(50), nullable=False,unique=True)
    join_time = db.Column(db.DateTime, nullable=False,default=datetime.utcnow)# utcnow表示当前数据的加入时间

class EmailCaptureModel(db.Model):
    __tablename__ = 'email_code'
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    email = db.Column(db.String(20), nullable=False)
    capture = db.Column(db.String(20), nullable=False)

class QuestionModel(db.Model):
    __tablename__ = 'question'
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, nullable=False,default=datetime.utcnow)
    # 外键
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    # 关系
    author = db.relationship(Usermodel, backref='qas')

class AnswerModel(db.Model):
    __tablename__ = 'answer'
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    content = db.Column(db.Text, nullable=False)
    create_time = db.Column(db.DateTime, nullable=False,default=datetime.utcnow)
    # 外键
    question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    #关系
    question = db.relationship(QuestionModel, backref=db.backref('answers', order_by=create_time.desc()))
    answer = db.relationship(Usermodel, backref='answers')